package comunicating.classes;

import java.io.IOException;
import java.io.PrintStream;
import noncomunicating.classes.Request;

public class Sender extends abstracts.Step{
	
	public Sender(Request request) {
		super(request);
		
		try {
			PrintStream textout = new PrintStream(request.getClientSocket().getOutputStream());
			
			//header
			textout.print("HTTP/1.1 " + request.getStatusCode() + " " + request.getStatus() + "\r\n");
			textout.print("Content-Type: " + request.getDataType(0) + "/" + request.getDataType(1) + "; charset=UTF-8\r\n");
			textout.print("Date: " + ServerLoop.getTimeString() +"\r\n");
			textout.print("Server: JW2S-1.0\r\n");

			textout.print("\r\n"); //end line
			//NOTE having improper newlines will break things in strange ways!
			
			if (!request.isHeadRequest()) { //only sends data when not a HEAD request
				request.getClientSocket().getOutputStream().write(request.getFileContent());
			}
			
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
	}

	@Override
	public void printOutput(boolean verbose) {
		System.out.println(getOutput(verbose));
		
	}

	@Override
	public String getOutput(boolean verbose) {
		String retString = null;
		if(verbose){
			retString += request.getName() + ": Data Sent!\n";
		}
		else{
			retString = "Data Sent.\n";
		}
		return retString;
	}
}
